package com.chenfan.magic.resolver;

import com.chenfan.magic.base.R;
import com.chenfan.magic.constant.RuntimeConstant;
import com.chenfan.magic.core.IdcEngineEntry;
import com.chenfan.magic.entity.TTLDetailModel;
import com.chenfan.magic.entity.TTLModel;
import com.chenfan.magic.tags.TagAbstract;
import com.chenfan.magic.tags.__TagProvider;
import com.chenfan.magic.utils.StringKit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * IDC 主解析引擎
 *
 * @author 周夕
 * @date 2020-02-24 18:44
 */
@Slf4j
@Data
public class _MainResolver {

    public static String decode(String scriptContext) {
        return scriptContext.replaceAll("&amp;", "&")
                .replaceAll("&enter;", "\n").replaceAll("&mtab;", "\t")
                .replaceAll("&lt;", "<").replaceAll("&gt;", ">")
                .replaceAll("&nbsp;", " ").replaceAll("&#39;", "'")
                .replaceAll("&quot;", "\"");
    }

    /**
     * 一级解析: jsonp
     *
     * @param idcRuntimePool
     * @return
     */
    public void resolve(Map<String, Object> idcRuntimePool, TTLModel ttlModel) {
        String scriptContext = idcRuntimePool.get(RuntimeConstant.RUNTIME_ORIGN_SCRIPT_CONTEXT).toString();
        //转码
        scriptContext = decode(scriptContext);
        IdcEngineEntry.takeRuntimeVar(idcRuntimePool, __TagProvider.jsonpProvider(scriptContext, ttlModel), RuntimeConstant.RUNTIME_MQL);
    }

    /**
     * 二级解析: 自定义解析引擎
     *
     * @param idcRuntimePool
     * @param ttlModel
     * @param prefixKey
     * @param tagAbstract
     * @param paramMap
     * @return
     */
    public String resolveScope(Map<String, Object> idcRuntimePool, TTLModel ttlModel, String prefixKey, TagAbstract tagAbstract, Map<String, Object> paramMap) {
        TTLDetailModel ttlDetailModel = new TTLDetailModel();
        ttlDetailModel.start(prefixKey);
        tagAbstract.setContext(tagAbstract.getResolveFail() != null ? tagAbstract.getResolveFail() : tagAbstract.getContext());
        if (StringKit.isNotBlank(tagAbstract.getContext()) && tagAbstract.getResolve()) {
            //解析方式获取: 1.isv 配置, 2.script配置, 3.标签属性配置
            __IResolver iResolver = __ResolverBuilder.getResolver(tagAbstract.getEngine());
            if (null == iResolver) {
                iResolver = __ResolverBuilder.getResolver(idcRuntimePool.get(RuntimeConstant.RUNTIME_ORIGN_SCRIPT_ENGINE) + "");
            }
            if (null == iResolver) {
                iResolver = __ResolverBuilder.getResolver(idcRuntimePool.get(RuntimeConstant.RUNTIME_ISV_ENGINE) + "");
            }
            if (null == iResolver) {
                IdcEngineEntry.throwException(idcRuntimePool, null, "非法解析器引擎类型");
            } else {
                String context = tagAbstract.getContext();
                context = context.replaceAll("<!--(.|[\r\n])*?-->", "");
                tagAbstract.setContext(iResolver.resolve(idcRuntimePool, context, paramMap));
            }
        }

        //解析器时间重新计算
        ttlModel.getResolver().put(ttlDetailModel.end()).caculate();
        return tagAbstract.getContext();
    }

    /**
     * 局部解析: 自定义解析引擎
     *
     * @param idcRuntimePool
     * @param templateContext
     * @param paramMap
     * @return
     */
    public R resolvePart(TTLModel ttlModel, Map<String, Object> idcRuntimePool, String templateContext, Map<String, Object> paramMap) {
        TTLDetailModel ttlDetailModel = new TTLDetailModel();
        ttlDetailModel.start();
        templateContext = decode(templateContext);
        String resolveContext = templateContext;
        __IResolver iResolver = null;
        String engine = "";
        if (StringKit.isNotBlank(templateContext)) {
            //解析方式获取: 1.isv 配置, 2.script配置, 3.标签属性配置
            engine = idcRuntimePool.get(RuntimeConstant.RUNTIME_ORIGN_SCRIPT_ENGINE) + "";
            iResolver = __ResolverBuilder.getResolver(engine);
            if (null == iResolver) {
                engine = idcRuntimePool.get(RuntimeConstant.RUNTIME_ISV_ENGINE) + "";
                iResolver = __ResolverBuilder.getResolver(idcRuntimePool.get(RuntimeConstant.RUNTIME_ISV_ENGINE) + "");
            }
            if (null == iResolver) {
                IdcEngineEntry.throwException(idcRuntimePool, null, "非法解析器引擎类型");
            } else {
                resolveContext = resolveContext.replaceAll("<!--(.|[\r\n])*?-->", "");
                resolveContext = iResolver.resolve(idcRuntimePool, templateContext, paramMap);
            }
        }
        ttlDetailModel.end();
        ttlModel.setResolver(ttlDetailModel);
        return R.success()
                .put("orign", templateContext)
                .put("engine", engine)
                .put("context", resolveContext)
                .put("params_request", idcRuntimePool.get(RuntimeConstant.RUNTIME_REQUEST_PARAMS))
                .put("params_tokenArrows", idcRuntimePool.get(RuntimeConstant.RUNTIME_TOKEN_ARROWS))
                .put("params_constant", idcRuntimePool.get(RuntimeConstant.RUNTIME_CONSTANTS));
    }

}
